import { total } from '@v/project-update/components/chnage-config'

const arr = [
  {
    label: '国家文保单位',
    val: '01',
  },
  {
    label: '省级文保单位',
    val: '02',
  },
  {
    label: '市级文保单位',
    val: '03',
  },
  {
    label: '区级文保单位',
    val: '04',
  },
  {
    label: '历史建筑',
    val: '05',
  },
  {
    label: '传统风貌建筑',
    val: '06',
  },
  {
    label: '国家一级古树',
    val: '07',
  },
  {
    label: '国家二级古树',
    val: '08',
  },
  {
    label: '国家三级古树',
    val: '09',
  },
  {
    label: '名木',
    val: '010',
  },
]

const arr2 = [
  {
    label: '年份',
    key: 'year',
  },
  {
    label: '实施内容',
    key: 'executeContent',
  },
  {
    label: '年度投资（万元）',
    key: 'ndtz',
  },
]

const dealPlan = (key, val) => {
  const result = []
  val.forEach((item) => {
    const keys = Object.keys(item)
    if (keys.length > 1) {
      keys.forEach((key) => {
        const value = item[key] + ''
        if (key === 'year') {
          if (value.includes('=')) {
            let { 0: oldVal, 1: newVal } = value.split('=')
            const name = '年份'
            result.push({
              name,
              oldVal,
              newVal,
            })
          }
        } else {
          const type = item['year']
          let name = ''
          if (type.includes('=')) {
            const { 1: newVal } = type.split('=')
            name = newVal === 'null' ? '' : newVal
          } else {
            name = type === 'null' ? '' : type
          }
          name += key === 'ndtz' ? '年度投资（万元）' : '实施内容'
          let { 0: oldVal, 1: newVal } = value?.split('=')
          oldVal = oldVal === 'null' ? '' : oldVal
          newVal = newVal === 'null' ? '' : newVal
          if (newVal || oldVal) {
            result.push({
              name,
              oldVal,
              newVal,
            })
          }
        }
      })
    }
  })
  return result
}

const getSpecialChange = (key, val) => {
  if (key === 'plan') {
    return dealPlan(key, val)
  }
  // console.log(key, val)
  const result = []
  val.forEach((item) => {
    const keys = Object.keys(item)
    if (keys.length > 1) {
      keys.forEach((key) => {
        const value = item[key] + ''
        if (key === 'type') {
          if (value.includes('=')) {
            let { 0: oldVal, 1: newVal } = value.split('=')
            const name = '类型'
            oldVal = arr.find((item) => item.val === oldVal)?.label || ''
            newVal = arr.find((item) => item.val === newVal)?.label || ''
            result.push({
              name,
              oldVal,
              newVal,
            })
          }
        } else {
          const type = item['type']
          let name = ''
          if (type.includes('=')) {
            const { 1: newVal } = type.split('=')
            name = arr.find((item) => item.val === newVal)?.label || ''
          } else {
            name = arr.find((item) => item.val === type)?.label || ''
          }
          name += key === 'area' ? '面积' : '数量'
          let { 0: oldVal, 1: newVal } = value?.split('=')
          oldVal = oldVal === 'null' ? '' : oldVal
          newVal = newVal === 'null' ? '' : newVal
          if (newVal || oldVal) {
            result.push({
              name,
              oldVal,
              newVal,
            })
          }
        }
      })
    }
  })
  return result
}

export const getLogContent = (fromJson) => {
  if (!fromJson) return []
  const children = []
  Object.entries(fromJson).forEach(([key, val]) => {
    if (Array.isArray(val)) {
      const arr = getSpecialChange(key, val)
      children.push(...arr)
      return
    }
    const { 1: newVal, 0: oldVal } = val?.split('=') || []
    const name = total.find((item) => item.key === key)?.label || key
    children.push({
      name,
      oldVal: oldVal,
      newVal: newVal,
    })
  })
  return children
}
